package com.xiaomei.manager.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.shiro.authz.permission.WildcardPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.xiaomei.manager.dao.SysResourceMapper;
import com.xiaomei.manager.model.SysResource;
import com.xiaomei.manager.service.IResourceService;

@Service
public class ResourceServiceImpl implements IResourceService {
	@Autowired
	private SysResourceMapper sysReMapper;
	
	public SysResource createResource(SysResource resource){
		if(sysReMapper.insert(resource) > 0){
	   		resource.setId(sysReMapper.selectLastInsertID());   		
    		return resource;
    	}else{
    		return null;
    	}
		
	}
    public SysResource updateResource(SysResource resource){
    	if(sysReMapper.updateByPrimaryKey(resource) > 0){
    		return resource;
    	}else{
    		return null;
    	}
    }
    
    public void deleteResource(Long resourceId){
    	SysResource resource = sysReMapper.selectByPrimaryKey(resourceId);
    	if(sysReMapper.deleteByPrimaryKey(resourceId) > 0){
    		sysReMapper.deleteByParentids(makeSelfAsParentIds(resource)+"%");
    	}        
    }
   
    public SysResource findOne(Long resourceId){
    	return sysReMapper.selectByPrimaryKey(resourceId);
    }
    
    public List<SysResource> findAll(){
    	return sysReMapper.selectAll();
    }

    /**
     * 得到资源对应的权限字符串
     * @param resourceIds
     * @return
     */
    public Set<String> findPermissions(Set<Long> resourceIds){
    	 Set<String> permissions = new HashSet<String>();
         for(Long resourceId : resourceIds) {
             SysResource resource = findOne(resourceId);
             if(resource != null && !StringUtils.isEmpty(resource.getPermission())) {
                 permissions.add(resource.getPermission());
             }
         }
         return permissions;
    }

    /**
     * 根据用户权限得到菜单
     * @param permissions
     * @return
     */
    public List<SysResource> findMenus(Set<String> permissions){
        List<SysResource> allResources = findAll();
        // 一级菜单资源
        Map<Long, SysResource> oneLevelMenus = new HashMap<Long, SysResource>();
        for(SysResource resource : allResources) {
            if(resource.isRootNode()) {
                continue;
            }
            if(resource.getType() != SysResource.ResourceType.menu) {
                continue;
            }
            if(!hasPermission(permissions, resource)) {
                continue;
            }
            if(resource.isOneLevelMenu()) {
            	SysResource re = oneLevelMenus.get(resource.getId());
            	if(re != null) {
            		resource.setLowerResources(re.getLowerResources());
            	}
            	oneLevelMenus.put(resource.getId(), resource);
            	continue;
            }
            
            // 获取父菜单
            SysResource re = oneLevelMenus.get(resource.getParentId());
            if(re == null) {
            	re = new SysResource();
            	oneLevelMenus.put(resource.getParentId(), re);
            }
            re.getLowerResources().add(resource);
        }
        
        return new ArrayList<SysResource>(oneLevelMenus.values());
    }
    
    private boolean hasPermission(Set<String> permissions, SysResource resource) {
        if(StringUtils.isEmpty(resource.getPermission())) {
            return true;
        }
        for(String permission : permissions) {
            WildcardPermission p1 = new WildcardPermission(permission);
            WildcardPermission p2 = new WildcardPermission(resource.getPermission());
            if(p1.implies(p2) || p2.implies(p1)) {
                return true;
            }
        }
        return false;
    }
    
    public String makeSelfAsParentIds(SysResource sys) {
        return sys.getParentIds() + sys.getId() + "/";
    }

}
